/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author pwarnimo
 */
public class DrawPanel extends JPanel { 
    private int coordX = 0;
    private int coordY = 0;
    private GateList Gates = new GateList();
    private ArrayList<String> Connections = new ArrayList();
    private DefaultListModel listModel;
    private int CurrentGateType = 0;
    private int GridSize = 10;
    private int CurrentID = 0;
    private boolean connect = false;
    private int tmpID;
    private String tmpConIO;
    
    public static final int GateSize = 45;
    
    public DrawPanel(DefaultListModel listModel) {
        super();
        setBackground(Color.white);
        setForeground(Color.black);
        this.addMouseListener(new CustomListener());
        this.listModel = listModel;
    }
    
    public void setCurrentGateType(int CurrentGateType) {
        this.CurrentGateType = CurrentGateType;
    }
    
    @Override
    public void paintComponent(Graphics g) {
        Graphics2D g2 = (Graphics2D)g;
        g2.clearRect(0, 0, this.getWidth(), this.getHeight());
        
        Gates.paintComponent(g2);
        
        for (int i = 0; i <= Connections.size() -1; i++) {
            String tmp = Connections.get(i);
            /*int ID1 = Integer.parseInt(tmp.substring(0, tmp.indexOf(",")));     
            int ID2 = Integer.parseInt(tmp.replace(tmp.substring(0, tmp.indexOf(",") +1), ""));
            
            g2.drawLine(Gates.getGate(ID1).getCoords().x, Gates.getGate(ID1).getCoords().y, Gates.getGate(ID2).getCoords().x, Gates.getGate(ID2).getCoords().y);
            //System.out.println(tmp.substring(0, tmp.indexOf(",")));
            //System.out.println(Connections.get(i));*/
            
            System.out.println(tmp);
        }
    }
     
    public class CustomListener extends MouseAdapter {
        private boolean gateSelected(int cX, int cY, int gX, int gY, int width) {
            if ((cX >= gX && cX <= (gX + width)) && (cY >= gY && cY <= (gY + width))) {
                return true;
            }
            else {
                return false;
            }
        }
        
        @Override
        public void mouseClicked(MouseEvent e) {
            coordX = e.getX();
            coordY = e.getY();
            
            if (e.isAltDown()) {
                int i = 0;
                
                while ((i < Gates.getCount()) && (gateSelected(coordX, coordY, Gates.getGate(i).getCoords().x, Gates.getGate(i).getCoords().y, 50) == false)) {
                    i++;
                    //System.out.println("Iteration " + i);
                }
                
                //System.out.println("I=" + i + " Count=" + Gates.getCount());
                
                if (i < Gates.getCount()) {
                    System.out.println("Selected gate id" + Gates.getGate(i).getID() + " at " + Gates.getGate(i).getCoords().x + "," + Gates.getGate(i).getCoords().y);
                    if (Gates.getGate(i) instanceof GateAnd) {
                        System.out.println("AND Gate");
                    }   
                    else if (Gates.getGate(i) instanceof GateNAnd) {
                        System.out.println("NAND Gate");
                    }
                    else if (Gates.getGate(i) instanceof GateOr) {
                        System.out.println("OR Gate");
                    }
                    else if (Gates.getGate(i) instanceof GateNOr) {
                        System.out.println("NOR Gate");
                    }
                    else if (Gates.getGate(i) instanceof GateXor) {
                        System.out.println("XOR Gate");
                    }
                    else if (Gates.getGate(i) instanceof GateXNor) {
                        System.out.println("XNOR Gate");
                    }
                    else if (Gates.getGate(i) instanceof GateNot) {
                        System.out.println("NOT Gate");
                    }
                    else if (Gates.getGate(i) instanceof PowerSource) {
                        System.out.println("Power source");
                    }
                    else if (Gates.getGate(i) instanceof Led) {
                        System.out.println("LED");
                        Led tmpLed = (Led)Gates.getGate(i);
                        tmpLed.toggle();
                    }
                    else if (Gates.getGate(i) instanceof ToggleSwitch) {
                        System.out.println("Switch");
                        ToggleSwitch tmpSwitch = (ToggleSwitch)Gates.getGate(i);
                        tmpSwitch.toggle();
                    }
                }
            }
            else if (e.isShiftDown()) {
                int i = 0;
                
                while ((i < Gates.getCount()) && (gateSelected(coordX, coordY, Gates.getGate(i).getCoords().x, Gates.getGate(i).getCoords().y, 50) == false)) {
                    i++;
                }
                
                if (i < Gates.getCount()) {
                    System.out.println("Selected gate id" + i + " at " + Gates.getGate(i).getCoords().x + "," + Gates.getGate(i).getCoords().y);
                    
                    if (connect) {
                        String conIO = JOptionPane.showInputDialog(null, "Which input or output?");
                        System.out.println(conIO);
                        System.out.println("Connecting gate ID" + tmpID + " [" + coordX + "," + coordY + "] to gate ID" + Gates.getGate(i).getID() + " [" + Gates.getGate(i).getCoords().x + "," + Gates.getGate(i).getCoords().y + "]");
                        Connections.add(tmpID + "," + tmpConIO + "," + Gates.getGate(i).getID() + "," + conIO);
                        Gates.getGate(tmpID).select();
                        connect = false;
                    }
                    else {
                        tmpConIO = JOptionPane.showInputDialog(null, "Which input or output?");
                        System.out.println(tmpConIO);
                        coordX = Gates.getGate(i).getCoords().x;
                        coordY = Gates.getGate(i).getCoords().y;
                        tmpID = Gates.getGate(i).getID();
                        Gates.getGate(i).select();
                        connect = true;
                    }
                }
            }
            else {

                switch (CurrentGateType) {
                    case 0 :
                        Gates.addGate(new GateAnd(new Point(coordX, coordY), CurrentID));
                        break;

                    case 1 :
                        Gates.addGate(new GateOr(new Point(coordX, coordY), CurrentID));
                        break;
                    
                    case 2 : 
                        Gates.addGate(new GateNot(new Point(coordX, coordY), CurrentID));
                        break;
                        
                    case 3 :
                        Gates.addGate(new GateXor(new Point(coordX, coordY), CurrentID));
                        break;
                        
                    case 4 :
                        Gates.addGate(new GateXNor(new Point(coordX, coordY), CurrentID));
                        break;
                        
                    case 5 :
                        Gates.addGate(new GateNAnd(new Point(coordX, coordY), CurrentID));
                        break;
                        
                    case 6 :
                        Gates.addGate(new GateNOr(new Point(coordX, coordY), CurrentID));
                        break;
                        
                    case 7 :
                        Gates.addGate(new ToggleSwitch(new Point(coordX, coordY), CurrentID));
                        break;
                        
                    case 8 : 
                        Gates.addGate(new Led(new Point(coordX, coordY), CurrentID));
                        break;
                        
                    case 9 :
                        Gates.addGate(new PowerSource(new Point(coordX, coordY), CurrentID));
                        break;
                }
 
                listModel.addElement(CurrentGateType + "," + CurrentID + "," + coordX + "," + coordY);
                CurrentID++;
            }
            
            repaint();
        }
    }
}